/*
 * @Author: chunlai
 * @Date: 2023-12-22 13:43:38
 * @LastEditTime: 2023-12-22 15:02:35
 * @LastEditors: minsheng_insurance_agency_zhoucl
 * @FilePath: \01.latest-short-insure\src\translate\validate\fields.js
 * @Description:投保常规必填字段校验
 */
import { userAge } from "@/methods/globals.js";
import { getDataFromUrl } from "@/utils/index";
import { getDetailBirthDay } from "@/methods/date.js";
import store from "@/store/index";
const pageStore = store.state.page,
  baosiName = getDataFromUrl("company");

export const tbgxRuleCheck = (userInfo, userType) => {
  let obj = {
    msg: "",
    target: "relationToAppnt",
    status: true,
  };
  const insertStore = store.state.insert,
    tbrObj = insertStore.applicant || {},
    sexVal = `${userInfo.sex}` || "",
    ageVal = userAge(userInfo.birthDate || "");

  let bbrTbgx = `${userInfo.relationToAppnt}`,
    trgAge = userAge(tbrObj.birthDate || ""),
    trgSex = `${tbrObj.sex}` || "",
    trgLabel = userType == "被保人" ? "投保人" : "被保人";
  if (!bbrTbgx) {
    obj.msg = `请选择${userType}的投保关系`;
    obj.status = false;
    return obj;
  }
  if (userType == "被保人") {
    if (ageVal < 8 && !["62", "45", "2"].includes(bbrTbgx)) {
      obj.msg = "被保人8周岁以下投保关系只能选择子女";
      obj.status = false;
      return obj;
    }
  }
  const ageDis = pageStore.ageInterval || 18;
  if (bbrTbgx == "1") {
    // 父母时，投保人年龄不能小于被保险人
    if (ageVal < trgAge) {
      obj.msg = `${userType}的投保关系选择父母时，${userType}年龄不能小于${trgLabel}`;
      obj.status = false;
      return obj;
    }
    // 非阻断
    if (ageVal - trgAge < ageDis) {
      obj.breakTips = `${userType}的投保关系选择父母时，年龄相差小于${ageDis}岁`;
    }
  } else if (bbrTbgx == "2") {
    // 关系为子女时， 子女年龄不能大于父母人；
    if (ageVal > trgAge) {
      obj.msg = `${userType}的投保关系选择子女时，${userType}年龄不能大于${trgLabel}`;
      obj.status = false;
      return obj;
    }
    // 非阻断
    if (trgAge - ageVal < ageDis) {
      obj.breakTips = `${userType}的投保关系选择子女时，年龄相差小于${ageDis}岁`;
    }
  } else if (bbrTbgx == "0") {
    // 关系为配偶时，性别不能相同；
    if (sexVal == trgSex) {
      obj.msg = `配偶关系，性别不能相同`;
      obj.status = false;
      return obj;
    }
  }
  return obj;
};

export const nameRuleCheck = (nameValue, userType) => {
  let obj = {
    msg: "",
    target: "name",
    status: true,
  };
  const maxNum = pageStore.nameMaxLength || 20,
    ruleChina = /^[\u4E00-\u9FA5][\u4E00-\u9FA5|·|-|•]*[\u4E00-\u9FA5]$/,
    ruleEng = /^[A-Za-z]*(\s[A-Za-z]*)*$/,
    regs = /\s\s/g; //2个连续空格

  if (nameValue === "" || !nameValue) {
    obj.msg = `${userType}姓名不能为空`;
    obj.status = false;
    return obj;
  }
  const isChina = isChinese(nameValue);
  if (isChina) {
    if (nameValue.length < 2) {
      obj.msg = `${userType}姓名少于两个汉字`;
      obj.status = false;
      return obj;
    } else if (nameValue.length > maxNum) {
      obj.msg = `${userType}姓名长度不能超过${maxNum}个汉字`;
      obj.status = false;
      return obj;
    } else if (!ruleChina.test(nameValue)) {
      obj.msg = `${userType}姓名输入有误`;
      obj.status = false;
      return obj;
    }
  } else {
    if (nameValue.length < 3) {
      obj.msg = `${userType}姓名小于3个字符`;
      obj.status = false;
      return obj;
    } else if (!ruleEng.test(nameValue) || regs.test(nameValue)) {
      obj.msg = `${userType}姓名输入有误`;
      obj.status = false;
      return obj;
    } else if (nameValue.length > 40) {
      obj.msg = `${userType}姓名长度超过最大限制`;
      obj.status = false;
      return obj;
    }
  }

  return obj;
};

const isChinese = (str) => {
  return escape(str).indexOf("%u") == 0;
};
export const idTypeRuleCheck = (userInfo, userType) => {
  let obj = {
    msg: "",
    status: true,
    target: "idType",
  };

  if (userInfo.idType === "") {
    obj.msg = `请选择${userType}证件类型`;
    obj.status = false;
    return obj;
  }
  const ageVal = userAge(userInfo.birthDate);
  if (userInfo.idType === "3") {
    if (ageVal >= 16) {
      obj.msg = `${userType}年满16周岁必须使用身份证，请更换证件`;
      obj.status = false;
      return obj;
    }
  }
  if (userInfo.idType === "11") {
    let gapAge = 4;
    if (ageVal >= gapAge) {
      obj.msg = `${userType}年满${gapAge}周岁不能使用出生证，请更换证件`;
      obj.status = false;
      return obj;
    }
  }
  return obj;
};
// 证件照校验
export const idFrontImgCheck = (userInfo, userType) => {
  let obj = {
    msg: "",
    status: true,
    target: "frontImg",
  };
  const notChkArr = pageStore.idImgNotCheck || [];
  if (!notChkArr.includes(userInfo.idType)) {
    if (userInfo.frontImg === "") {
      obj.msg = `请上传${userType}证件照`;
      obj.status = false;
      return obj;
    }
  }
  return obj;
};
export const phoneRuleCheck = (userInfo, userType) => {
  let obj = {
    msg: "",
    status: true,
    target: "phone",
  };
  if (userInfo.phone == "") {
    obj.msg = `请输入${userType}手机号`;
    obj.status = false;
    return obj;
  }
  const validphone = /^1[3456789]\d{9}$/;
  if (!validphone.test(userInfo.phone)) {
    obj.msg = `${userType}手机号错误`;
    obj.status = false;
    return obj;
  }
  return obj;
};
export const birthdayRuleCheck = (userInfo, userType) => {
  let obj = {
    msg: "",
    status: true,
    target: "birthDate",
  };
  if (userInfo.birthDate === "") {
    obj.msg = `请选择${userType}出生日期`;
    obj.status = false;
    return obj;
  }
  //只校验投保人
  if (userType == "被保人") {
    obj = rangeValide(userInfo, userType);
  }
  return obj;
};
export const rangeValide = (userInfo, userType) => {
  let obj = {
    msg: "",
    status: true,
    target: "birthDate",
  };
  const userBirth = `${userInfo.birthDate}`,
    appliAge = userAge(userBirth);
  if (["0", "3"].includes(userInfo.idType)) {
    obj.target = "idNum";
  }
  const infoStore = store.state.infoReg,
    rageObj = infoStore.ageRange;
  let ageCheckReg = rageObj.insured;
  if (userType == "投保人") {
    ageCheckReg = rageObj.applicant;
  }
  if (ageCheckReg.isDate) {
    // 计算天数
    if (
      appliAge > ageCheckReg.maxAge ||
      getDetailBirthDay(userBirth) < ageCheckReg.minAge
    ) {
      obj.msg = `${userType}年龄需在${ageCheckReg.minAge}天到${ageCheckReg.maxAge}岁之间`;
      obj.status = false;
      return obj;
    }
  } else {
    if (appliAge > ageCheckReg.maxAge || appliAge < ageCheckReg.minAge) {
      obj.msg = `${userType}年龄需在${ageCheckReg.minAge}到${ageCheckReg.maxAge}岁之间`;
      obj.status = false;
      return obj;
    }
  }
  return obj;
};
// 告知校验
export const noticeRuleCheck = (userInfo, userType) => {
  let obj = {
    msg: "",
    target: "healthyStatus",
    status: true,
  };
  if (!userInfo.healthyStatus) {
    obj.msg = `请确认${userType}健康告知`;
    obj.status = false;
    return obj;
  }
  if (userInfo.healthyStatus == "N") {
    obj.msg = `${userType}健康告知不通过，不可投保`;
    obj.status = false;
    return obj;
  }
  return obj;
};
// export const funName = () => {};
// export const funName = () => {};
// export const funName = () => {};
